package com.sinosoft.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sinosoft.common.Const;
import com.sinosoft.entity.DefPrivilege;
import com.sinosoft.entity.DefRole;
import com.sinosoft.entity.DefUser;
import com.sinosoft.mapper.DefPrivilegeMapper;
import com.sinosoft.mapper.DefRoleMapper;
import com.sinosoft.mapper.DefUserMapper;
import com.sinosoft.model.EasyUITree;
import com.sinosoft.service.IHomeService;
import com.sinosoft.util.MD5Util;

@Service
public class HomeServiceImpl implements IHomeService {

	@Autowired
	private DefUserMapper userMapper;
	
	@Autowired
	private DefRoleMapper roleMapper;
	
	@Autowired
	private DefPrivilegeMapper privilegeMapper;
	
	@Override
	public Map<String, Object> login(DefUser user, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		if (null == user || StringUtils.isBlank(user.getUserCode()) 
				|| StringUtils.isBlank(user.getPassword())) {
			resultMap.put("flag", false);
			resultMap.put("msg", "输入的登录信息为空！");
			return resultMap;
		}
		user.setPassword(MD5Util.md5(user.getPassword()));
		List<DefUser> userList = userMapper.selectByCondition(user);
		if (null == userList) {
			resultMap.put("flag", false);
			resultMap.put("msg", "用户不存在或密码错误！");
		} else if (userList.size() <= 0) {
			resultMap.put("flag", false);
			resultMap.put("msg", "用户不存在或密码错误！");
		} else if (userList.size() > 1) {
			resultMap.put("flag", false);
			resultMap.put("msg", "用户数据异常，不唯一！");
		} else if (userList.size() == 1) {
			List<DefRole> roleList = new ArrayList<DefRole>();
			roleList = roleMapper.selectByUser(user);
			Iterator<DefRole> roleIt = roleList.iterator();
			while (roleIt.hasNext()) {
				DefRole r = roleIt.next();
				List<DefPrivilege> pList = new ArrayList<DefPrivilege>();
				pList = privilegeMapper.selectByRole(r);
				r.setPrivilegeList(pList);
			}
			userList.get(0).setRoleList(roleList);
			resultMap.put("flag", true);
			resultMap.put("msg", "登录成功！");
			request.getSession(true).setAttribute(Const.SESSION_USER_KEY, userList.get(0));
		}
		return resultMap;
	}

	@Override
	public List<EasyUITree> queryPrivilegeByPId(Long pid, HttpServletRequest request) {
		DefUser user = (DefUser) request.getSession().getAttribute(Const.SESSION_USER_KEY);
		Iterator<DefRole> roleIt = user.getRoleList().iterator();
		Set<DefPrivilege> privilegeSet = new HashSet<DefPrivilege>();
		while (roleIt.hasNext()) {
			privilegeSet.addAll(roleIt.next().getPrivilegeList());
		}
		return constructEasyUITree(pid, privilegeSet);
	}
	
	/**
	 * 构造EasyUI格式的树形菜单的对象
	 * @param pid
	 * @return
	 */
	private List<EasyUITree> constructEasyUITree(Long pid, Set<DefPrivilege> privilegeSet) {
		List<EasyUITree> treeList = new ArrayList<EasyUITree>();
		Iterator<DefPrivilege> privilegeIt = privilegeSet.iterator();
		while (privilegeIt.hasNext()) {
			DefPrivilege p = privilegeIt.next();
			if (pid == p.getPid()) {
				EasyUITree tree = new EasyUITree();
				tree.setId(p.getPrivilegeId());
				tree.setText(p.getPrivilegeName());
				tree.setIconCls(p.getIcon());
				Map<String, Object> attr = new HashMap<String, Object>();
				attr.put("pId", p.getPid());
				attr.put("privilegeCode", p.getPrivilegeCode());
				attr.put("privilegeType", p.getPrivilegeType());
				attr.put("url", p.getUrl());
				tree.setAttributes(attr);
				tree.setChildren(constructEasyUITree(p.getPrivilegeId(), privilegeSet));
				treeList.add(tree);
			}
		}
		return treeList;
	}

}
